<?php
require_once "db_connection21.php";

class InfoManager{
    private $conn;

    public function __construct(){
        global $conn;
        $this->conn = $conn;
    }

    public function addInfo($name, $email, $phone, $address, $hobby){
        $sql = "INSERT INTO info (name, email, phone, address, hobby) VALUES ('$name', '$email', '$phone', '$address', '$hobby')";
        if ($this->conn->query($sql) === TRUE) {
            return true;
        } else {
            return false;
        }
    }

    public function getInfos(){
        $infos = [];
        
        $sql = "SELECT * FROM info";
        $result = $this->conn->query($sql);
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                $infos[] = $row;
            }
        }
        return $infos;
    }

    public function getInfoById($id){
        $sql = "SELECT * FROM info WHERE id = $id";
        $result = $this->conn->query($sql);
        if ($result->num_rows > 0) {
            return $result->fetch_assoc();
        }
        return null;
    }

    public function __destruct(){
        $this->conn->close();
    }

    public function updateInfo($id, $name, $email, $phone, $address, $hobby){
        $sql = "UPDATE info SET name = '$name', email = '$email', phone = '$phone', address = '$address', hobby = '$hobby' WHERE id = $id";
        if ($this->conn->query($sql) === TRUE) {
            return true;
        } else {
            return false;
        }
    }

    public function deleteInfo($id){
        $sql = "DELETE FROM info WHERE id = $id";
        if ($this->conn->query($sql) === TRUE) {
            return true;
        } else {
            return false;
        }
    }
}